package com.txhy.certificatechain.presenter.user;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.txhy.certificatechain.R;
import com.txhy.certificatechain.api.UrlAddress;
import com.txhy.certificatechain.presenter.base.BasePresenter;
import com.txhy.certificatechain.ui.view.user.LoginView;
import com.txhy.certificatechain.utils.GsonUtil;
import com.txhy.certificatechain.utils.UIUtils;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: LoginRegistPresenter.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 登录注册
 * 创建时间: 2018/9/20 15:52
 * 更新时间：2018/9/20 15:52
 * -----------------------------------------------------------------
 * 2018/9/20 15:52: 创建 LoginRegistPresenter.java (侯建军);
 * -----------------------------------------------------------------
 */
public class LoginRegistPresenter extends BasePresenter<LoginView> {
    /**
     * 进行登录
     * @param phone
     * @param meid
     * @param code
     */
    public void toLogin(String phone, String meid, String code) {
        getView().showProgress("正在登录...");
        OkGo.<String>post(UrlAddress.PHONECHECK_URL)//
                .tag(this)//
                .params("phone", phone)
                .params("meid", meid)
                .params("verifiCode", code)

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().loginSuccess(GsonUtil.GsonGetData(response.body()));
                        } else {
                            getView().loginFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().loginFail("请求失败请检查网络");
                        getView().hideProgress();
                    }
                });
    }
    /**
     * 发送短信
     * @param phone
     * @param meid
     */
    public void SendCode(String phone,String meid) {
        getView().showProgress(UIUtils.getString(R.string.sendcodeok));
        OkGo.<String>get(UrlAddress.GETLOGINV_URL)//
                .tag(this)//
                .params("phone", phone)
                .params("meid", meid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().sendCodeSuccess(GsonUtil.GsonGetMsg(response.body()));
                        } else {
                            getView().sendCodeFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().sendCodeFail("请求失败请检查网络");
                        getView().hideProgress();
                    }
                });
    }

    /**
     * 指纹登录
     * @param phone
     * @param meid
     * @param type
     * @param faceUrl
     */
    public void toFingerLogin(String phone, String meid, String type,String faceUrl) {
        getView().showProgress(UIUtils.getString(R.string.zaidianjia));
        OkGo.<String>post(UrlAddress.BIOLOGYCHECK_URL)//
                .tag(this)//
                .params("phone", phone)
                .params("meid", meid)
                .params("type", type)
                .params("faceUrl", faceUrl)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().fingerSuccess(GsonUtil.GsonGetData(response.body()));
                        } else {
                            getView().fingerFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().fingerFail("请求失败请检查网络");
                        getView().hideProgress();
                    }
                });
    }

    /**
     *
     * @param phone
     * @param numberCode
     */
    public void toEnd(String phone, String numberCode) {
        getView().showProgress("正在登录...");
        OkGo.<String>post(UrlAddress.USERLOGIN_URL)//
                .tag(this)//
                .params("phone", phone)
                .params("numberCode", numberCode)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().endSuccessSuccess(response.body());
                        } else {
                            getView().endFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().endFail("请求失败请检查网络");
                        getView().hideProgress();
                    }
                });
    }
}
